﻿/// <reference path="_namespace.js" />
/// <reference path="Shape.js" />
/// <reference path="../../lang/Formats.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.geometry.shapes.Rectangle
/*****************************************************************************/

ajax.lang.addNamespace("ajax.geometry.shapes");

//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle = function(x, y, width, height) // Constructor
{
    ajax.geometry.shapes.Rectangle.superConstructor.call(this);
    
    this.x      = x || 0;
    this.y      = y || 0;
    this.width  = width || 0;
    this.height = height || 0;
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.geometry.shapes.Rectangle", "ajax.geometry.shapes.Shape"); // Inherit
if ($getDesignMode())
    ajax.geometry.shapes.Rectangle.prototype = ajax.geometry.shapes.Shape.prototype;

// For ms ajax
ajax.geometry.shapes.Rectangle.prototype.x = 0;
ajax.geometry.shapes.Rectangle.prototype.y = 0;
ajax.geometry.shapes.Rectangle.prototype.width = 0;
ajax.geometry.shapes.Rectangle.prototype.height = 0;

// Overrides
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.intersects = function(rect1, rect2)
{///<summary>Checks for the collision of two Rectangles.</summary>
 ///<returns type="boolean"/>
    if (
        (rect1.x < box2.x + box2.width) &&
        (rect1.x + rect1.width > box2.x) &&
        (rect1.y + rect1.height < box2.y) &&
        (rect1.y > rect2.y + rect2.height)
        )    
        return true;
        
    return false;
}
ajax.geometry.shapes.Rectangle.prototype.intersects = function(rect)
{///<summary>Checks for collision with another rectangle.</summary>
 ///<returns type="boolean"/>
    return ajax.geometry.shapes.Rectangle.intersects(this, rect);    
}



//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.multiplyPositionScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x * scalarValue,
        rect.y * scalarValue,
        rect.width,
        rect.height
    );
}
ajax.geometry.shapes.Rectangle.prototype.multiplyPositionScalar = function(scalarValue)
{
    this.x *= scalarValue;
    this.y *= scalarValue;
}
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.multiplySizeScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x, rect.y,
        rect.width * scalarValue,
        rect.height * scalarValue
    );
}
ajax.geometry.shapes.Rectangle.prototype.multiplySizeScalar = function(scalarValue)
{
    this.width *= scalarValue;
    this.height *= scalarValue;
}
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.multiplyScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x * scalarValue,
        rect.y * scalarValue,
        rect.width * scalarValue,
        rect.height * scalarValue
    );
}

ajax.geometry.shapes.Rectangle.prototype.multiplyScalar = function(scalarValue)
{
    this.x *= scalarValue;
    this.y *= scalarValue;
    this.width *= scalarValue;
    this.height *= scalarValue;    
}
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.dividePositionScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x / scalarValue,
        rect.y / scalarValue,
        rect.width,
        rect.height
    );
}
ajax.geometry.shapes.Rectangle.prototype.dividePositionScalar = function(scalarValue)
{
    this.x /= scalarValue;
    this.y /= scalarValue;
}
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.divideSizeScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x, rect.y,
        rect.width / scalarValue,
        rect.height / scalarValue
    );
}
ajax.geometry.shapes.Rectangle.prototype.divideSizeScalar = function(scalarValue)
{
    this.width /= scalarValue;
    this.height /= scalarValue;
}
//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.divideScalar = function(rect, scalarValue)
{
    return new ajax.geometry.shapes.Rectangle(
        rect.x / scalarValue,
        rect.y / scalarValue,
        rect.width / scalarValue,
        rect.height / scalarValue
    );
}

ajax.geometry.shapes.Rectangle.prototype.divideScalar = function(scalarValue)
{
    this.x /= scalarValue;
    this.y /= scalarValue;
    this.width /= scalarValue;
    this.height /= scalarValue;    
}

//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.prototype.toString = function()
{
    $imports("ajax.lang.Formats");
    var f = ajax.lang.Formats.currency;
    return String.toFormat("x: {0}, y: {1}, width: {2}, height: {3}",
        f(this.x), f(this.y), f(this.width), f(this.height)
    );
}

//-----------------------------------------------------------------------------
ajax.geometry.shapes.Rectangle.cast = function(obj)
{///<returns type="ajax.geometry.shapes.Rectangle"/>
    return obj;    
}

